package operation;

import book.Book;
import book.BookList;

import java.math.BigDecimal;
import java.util.Scanner;

public class AddOperation extends SearchOperation implements IOperation{

    public void add(BookList bookList,Book book){
        bookList.checkCapacity();
        int usedSize = bookList.getUsedSize();
        bookList.setBook(usedSize,book);
        bookList.setUsedSize(usedSize+1);
    }

    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入按提示输入要添加的书的信息: ");
        System.out.println("请输入书名: ");
        String bookName = scanner.nextLine();
        int index = search(bookList,bookName);
        if(index != -1) {
            System.out.println("书架上已有图书《"+bookName+"》,请重新输入!");
            return;
        }
        System.out.println("请输入书的价格: ");
        String priceStr = scanner.nextLine();
        BigDecimal price = new BigDecimal(priceStr);
        System.out.println("请输入书的作者: ");
        String author = scanner.nextLine();
        System.out.println("请输入书的类型: ");
        String type = scanner.nextLine();

        add(bookList,new Book(bookName,author,price,type));
        System.out.println("添加成功!");
    }
}
